package cn.zk.solution.coder;

import cn.zk.solution.protocol.MessageProtocol;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.MessageToByteEncoder;

/**
 * 编码器
 */
public class MyMessageEncoder extends MessageToByteEncoder<MessageProtocol> {
    /**
     *
     * @param ctx:上下文
     * @param msg:自定义协议包
     * @param out:
     * @throws Exception
     */
    @Override
    protected void encode(ChannelHandlerContext ctx, MessageProtocol msg, ByteBuf out) throws Exception {
        System.out.println("----encoder----");
        // 发送长度
        out.writeInt(msg.getLen());
        // 发送内容
        out.writeBytes(msg.getContent());
    }
}
